﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model.ModelEventArgs;

namespace View.Students
{
    /// <summary>
    /// Interaction logic for InputAddressPanel.xaml
    /// </summary>
    public partial class InputAddressPanel
    {
        public event EventHandler<AddressEventArgs> ApplyInputAddressEvent;
        public event EventHandler CancelInputAddressEvent;

        public InputAddressPanel()
        {
            InitializeComponent();
            streetType.ItemsSource = Data.DataDispatcher.GetInstance().GetAllStreetTypes();
            country.ItemsSource = Data.DataDispatcher.GetInstance().Countreis;
          //  city.ItemsSource = Data.DataDispatcher.GetInstance().Countreis

            streetType.SelectedIndex = 0;
        }

        private void applyInputAddressButtonClick(object sender, RoutedEventArgs e)
        {
            Model.ModelClasses.Street newStreet = new Model.ModelClasses.Street();
            newStreet.Town = (Model.ModelClasses.Town)city.SelectedValue;
            newStreet.StreetType = streetType.SelectedValue.ToString();
            newStreet.Name = (string)street.SelectedValue;

            Model.ModelClasses.HomeAddress newAddress = new Model.ModelClasses.HomeAddress
            {
                Building = buildingTextBox.Text,
                Flat = short.Parse(flatTextBox.Text),
                Street = newStreet
            };

            if (ApplyInputAddressEvent != null) 
                ApplyInputAddressEvent(this, new AddressEventArgs(newAddress)); 
        }

        private void cancelInputAddressButtonClick(object sender, RoutedEventArgs e)
        { if (CancelInputAddressEvent != null) CancelInputAddressEvent(this, new EventArgs()); }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            house.IsEnabled = house.IsChecked.Value == true;
        }

        private void country_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            city.ItemsSource = ((Model.ModelClasses.Country)country.SelectedItem).Towns;
        }

        private void city_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            street.ItemsSource = ((Model.ModelClasses.Town)city.SelectedItem).Streets;
        }

        private void street_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            streetType.SelectedItem = ((Model.ModelClasses.Street)street.SelectedItem).StreetType;
        }

    }
}
